{extend name="layout/default" /}
{block name="title"}版本修改{/block}
{block name="content"}
<form class="layui-form" action="">
    <input type="hidden" name="id" value="{$info.id}">
    <div class="mainBox" style="background-color: white;">
        <div class="main-container">
            <div class="main-container">
                <div class="layui-form-item">
                    <label class="layui-form-label">软件名称</label>
                    <div class="layui-input-block">
                        <select name="sid" lay-search lay-filter="getSoftwareInfo" disabled>
                            <option value="{$info.sid}">{$info.software.name}</option>
                        </select>
                    </div>
                </div>
                <div class="layui-form-item" id="configView">
                    {volist name="info.software.config" id="vo" empty="无配置内容"}
                        {if($vo.type == "text")}
                            <div class="layui-form-item">
                                <label class="layui-form-label">{$vo.cnname}</label>
                                <div class="layui-input-block">
                                    <input type="text" name="config[{$vo.enname}]" placeholder="" autocomplete="off" class="layui-input" value="{$info.config[$vo.enname] | default=''}">
                                </div>
                            </div>
                        {elseif($vo.type == "textarea") /}
                            <div class="layui-form-item">
                                <label class="layui-form-label">{$vo.cnname}</label>
                                <div class="layui-input-block">
                                    <textarea name="config[{$vo.enname}]" class="layui-textarea">{$info.config[$vo.enname] | htmlspecialchars_decode | default = '' }</textarea>
                                </div>
                            </div>
                        {elseif($vo.type == "radio") /}
                            <div class="layui-form-item">
                                <label class="layui-form-label">{$vo.cnname}</label>
                                <div class="layui-input-block">
                                    <input type="radio" name="config[{$vo.enname}]" value="1" title="是" {if(isset($info.config[$vo.enname])) AND ($info.config[$vo.enname] == 1)}checked{/if}>
                                    <input type="radio" name="config[{$vo.enname}]" value="0" title="否" {if(isset($info.config[$vo.enname])) AND ($info.config[$vo.enname] == 0)}checked{/if}>
                                </div>
                            </div>
                        {elseif($vo.type == "file") /}
                            <div class="layui-form-item layui-row">
                                <label class="layui-form-label">{$vo.cnname}</label>
                                <div class="layui-input-block layui-upload layui-col-space5">
                                    <div class="layui-col-xs12 layui-col-sm8">
                                        <input type="text" name="config[{$vo.enname}]" placeholder="点击按钮上传文件" autocomplete="off" readonly class="layui-input" value="{$info.config[$vo.enname] | default=''}">
                                    </div>
                                    <div class="layui-col-xs12 layui-col-sm4">
                                        <button type="button" class="pear-btn pear-btn-primary upload" data-name="{$vo.enname}" id="upload">上传文件</button>
                                    </div>
                                </div>
                            </div>
                        {/if}
                    {/volist}
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">状态</label>
                    <div class="layui-input-block">
                        <input type="radio" name="status" value="0" title="推送" checked>
                        <input type="radio" name="status" value="1" title="不推送">
                    </div>
                </div>
            </div>
        </div>
    </div>
    <div class="bottom">
        <div class="button-container">
            <button type="submit" class="pear-btn pear-btn-primary pear-btn-sm" lay-submit="" lay-filter="form-save">
                <i class="layui-icon layui-icon-ok"></i>
                提交
            </button>
            <button type="reset" class="pear-btn pear-btn-sm">
                <i class="layui-icon layui-icon-refresh"></i>
                重置
            </button>
        </div>
    </div>
</form>
{/block}
{block name="script"}
<script>
    layui.use(['form','jquery', 'popup', 'upload', 'button'],function(){
        let form = layui.form;
        let $ = layui.jquery;
        let popup = layui.popup;
        let upload = layui.upload;
        let button = layui.button;

        let MODULE_PATH = "/{:request()->controller()}";

        form.on('submit(form-save)', function(data){
            let loading = layer.load();
            $.ajax({
                url: MODULE_PATH + "/edit",
                data: data.field,
                dataType:'json',
                type:'post',
                success:function(res){
                    layer.close(loading);
                    if(Number(res.code) === 0){
                        popup.success(res.msg, function() {
                            parent.layer.close(parent.layer.getFrameIndex(window.name));//关闭当前页
                            parent.layui.table.reload("dataTable");
                        });
                    }else{
                        layer.close(loading);
                        popup.failure(res.msg);
                    }
                },
                error: function (res) {
                    layer.close(loading);
                    popup.failure("网络错误, 请重试");
                }
            })
            return false;
        });

        // 初始化上传事件
        function initUpload() {
            let upload_btn_load
                ,that
                ,name;
            upload.render({
                elem: ".upload"
                ,url: '/upload/add'
                ,auto: true
                ,accept: "file"
                ,before: function (obj) {
                    upload_btn_load = button.load({
                        elem:'.upload',
                    });
                }
                ,progress: (n, elem) => {
                    // 获取当前实例 dom
                    that = elem;
                    name = $(that).data("name");
                }
                ,done: (res, index, upload) => {
                    upload_btn_load.stop();
                    if(Number(res.code) === 0) {
                        $("input[name='config["+name+"]']").val(res.data.src);
                        popup.success(res.msg);
                    } else {
                        popup.failure(res.msg);
                    }
                }
                ,error: function () {
                    upload_btn_load.stop();
                }
            });
        }
        initUpload();

        let tp_debug = $("#think_page_trace_open");
        (tp_debug.length > 0) && tp_debug.remove();
    })
</script>
{/block}